package com.xnx3.banmahuishou.wangge.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnx3.BaseVO;
import com.xnx3.DateUtil;
import com.xnx3.banmahuishou.core.entity.Agency;
import com.xnx3.banmahuishou.core.entity.MoneyChangeRecord;
import com.xnx3.banmahuishou.core.entity.Qishou;
import com.xnx3.banmahuishou.core.entity.Wangge;
import com.xnx3.banmahuishou.core.util.AppUtil;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;

/**
 * 财务管理
 * @author 王晓龙
 */
@Controller(value="BanmahuishouWanggeFinanceController")
@RequestMapping("/wangge/finance/")
public class FinanceController extends BaseController  {
    @Resource
    private SqlService sqlService;

    /**
     * 分配金额列表
     */
    @RequestMapping("list${url.suffix}")
    public String list(HttpServletRequest request,Model model){
        Sql sql = new Sql(request);
        sql.setSearchColumn(new String[]{"name"});
        sql.appendWhere("wangge_userid = " + getUserId());
        int count = sqlService.count("laji_qishou", sql.getWhere());
        Page page = new Page(count, 15, request);
        sql.setSelectFromAndPage("SELECT * FROM laji_qishou", page);
        sql.setDefaultOrderBy("laji_qishou.id DESC");
        List<Qishou> list = sqlService.findBySql(sql, Qishou.class);
        String sqlAgency = "SELECT * FROM laji_wangge WHERE id=" + getUserId();
        Wangge wangge = sqlService.findAloneBySqlQuery(sqlAgency, Wangge.class);

        ActionLogUtil.insert(request, "财务管理", "第"+page.getCurrentPageNumber()+"页");

        model.addAttribute("money",wangge.getMoney());
        model.addAttribute("page", page);
        model.addAttribute("list", list);
        return "/wangge/finance/list";
    }

    /**
     * 给骑手设置金额
     * @param id 
     */
    @RequestMapping("edit${url.suffix}")
    public String edit(
        @RequestParam(value = "id", required = false, defaultValue="0") int id,
        Model model, HttpServletRequest request){
        Qishou entity;

        //查询骑手
        entity = sqlService.findById(Qishou.class, id);
        if(entity == null){
            return error(model, "要设置金额的骑手不存在");
        }
        ActionLogUtil.insert(request, "进入设置金额页面", entity.toString());

        //编辑页面
        model.addAttribute("entity", entity);
        return "/wangge/finance/edit";
    }

    /**
     * 给骑手设置的金额进行保存
     */
    @RequestMapping(value="save.json", method = RequestMethod.POST)
    @ResponseBody
    public BaseVO variableSave(Qishou inputEntity, HttpServletRequest request){
        Qishou entity = null;	
        Integer danQuota = inputEntity.getDanQuota();
        Integer dayQuota = inputEntity.getDayQuota();
        // 微信官方默认设置是 单笔单日限额200元
        if (danQuota - 20000 > 0) {
            return error("单笔单日限额200元");
        }
        if (danQuota - 100 < 0) {
            return error("每单限额须大于1元");
        }
        if (dayQuota - 100 < 0) {
            return error("每日限额须大于1元");
        }
        //设置
        entity = sqlService.findById(Qishou.class, inputEntity.getId());
        if(entity == null){
            return error("要设置金额的目标不存在");
        }

        //保存
        entity.setDanQuota(danQuota);
        entity.setDayQuota(dayQuota);	
        sqlService.save(entity);

        ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "保存设置金额", entity.toString());
        return success();
    }
}
